var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");

/* GET home page. */
router.post('/', upload.array(), function(req, res, next) {
    let eventid = _.trim(String(req.body.eventid));
    let solution = _.trim(String(req.body.solution));
    let data = {
        isSuccess: false,
        data: null,
        msg: '事件处理失败！'
    };

    let sql = "UPDATE event_record SET isfinished=1, finisheddate='" 
    + new Date().toLocaleDateString() +"', solution='" 
    + solution + "' WHERE id=" + eventid;
    let sql2 = "UPDATE equipmentinfo SET state=1 WHERE id = (SELECT equipmentid FROM event_record WHERE id = " + eventid + " )"
    console.log('sql:' + sql)
    console.log("sql2:" + sql2)
    db.execute(sql, function(err, rows) {
        if(err) {
            return res.json(data);
        } else {
            if(rows.affectedRows > 0 ) {
                console.log(rows)
                // data.data = rows;
                // if(rows.zcbh) {
                    db.execute(sql2, function(err1, rows1) {
                        if(err1) {
                            return res.json(data);
                        } else {
                            data.isSuccess = true;
                            data.msg = "事件处理成功！";
                            return res.json(data);
                        }
                    })
                // } else {
                    // data.isSuccess = true;
                    // data.msg = "事件处理成功！";
                    // return res.json(data);
                // }
            } else {
                return res.json(data);
            }
        }
    })
});

module.exports = router;